package com.pw.ba.result;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;

import java.util.ArrayList;
import java.util.List;

/**
 * @Auther: zhengzihang
 * @Date: 2018/7/4 15:20
 * @Description: {
 * code: 0,
 * msg: "",
 * count: 1000,
 * data: []
 * }
 * id
 * value
 * label
 * type
 * description
 * sort
 * parentId
 * createBy
 * createDate
 * updateBy
 * updateDate
 * remarks
 * delFlag
 */
@ApiModel("公共响应对象")
@EqualsAndHashCode(callSuper=false)
@Data
@Accessors(chain = true)
public class ApiResult<T> extends BaseTableResult<T> {
    /**
     * 总数
     */
    @ApiModelProperty(value = "数据总条数", name = "total", required = false)
    private Long total;
    /**
     * 总页数
     */
    @ApiModelProperty(value = "总页数", name = "pages", required = false)
    private Long pages;
    /**
     * 每页显示条数，默认 10
     */
    @ApiModelProperty(value = "每页显示条数，默认 10", name = "size", required = false)
    private Integer size;
    /**
     * 当前页
     */
    @ApiModelProperty(value = "当前页", name = "current", required = false)
    private Integer current;


    public ApiResult() {
        this.code = SUCCESS;
        this.msg = "ok";
    }

    public ApiResult(String msg, T data) {
        this.code = ERROR;
        this.msg = msg;
        this.data = data;
    }

    public ApiResult(T data, Long count) {
        this();
        this.data = data;
        this.setTotal(count);
    }

    public ApiResult(T data) {
        this();
        this.data = data;
    }


    public ApiResult(List data) {
        this();
        if (data == null) {
            data = new ArrayList();
        }
        this.setTotal(Long.valueOf(data.size()));
        this.data = (T) data;
    }

    public ApiResult(String msg) {
        this.code = ERROR;
        this.msg = msg;
    }

    public static ApiResult Result(Boolean b) {
        if (b) {
            return new ApiResult();
        }
        return ErrorResult("error");
    }

    public static ApiResult Result(int row) {
        if (row > 0) {
            return new ApiResult();
        }
        return ErrorResult("error:row = 0");
    }

    public static ApiResult Result(Object data) {
        return new ApiResult(data);
    }

    public static ApiResult Result(List data) {
        return new ApiResult(data);
    }

    public static ApiResult Result(List data, Integer start, Integer count) {
        ApiResult apiResult = new ApiResult(data);
        apiResult.setCurrent(start);
        apiResult.setSize(count);
        return apiResult;
    }

    public static ApiResult Result(String msg) {
        return new ApiResult(msg);
    }

    public static ApiResult OKResult() {
        return new ApiResult();
    }

    public static ApiResult OKResult(Object data) {
        return new ApiResult(data);
    }


    public static ApiResult OKResult(String msg) {
        ApiResult result = new ApiResult();
        result.setMsg(msg);
        return result;
    }

    public static ApiResult DateResult(String data) {
        ApiResult result = new ApiResult();
        result.setMsg("ok");
        result.setData(data);
        return result;
    }

    public static ApiResult ErrorResult(String msg) {
        return new ApiResult(msg);
    }

    public static ApiResult ErrorResult(String msg, Object data) {
        return new ApiResult(msg, data);
    }

    public static ApiResult ErrorResult() {
        return new ApiResult("ERROR");
    }

    public static ApiResult ErrorResult(Throwable t) {
        return new ApiResult(t.getMessage());
    }

    public static ApiResult ErrorResult(String msg, String erroMsg) {
        ApiResult result = new ApiResult();
        result.setCode(ERROR);
        result.setMsg(msg);
        result.setErroMsg(erroMsg);
        return result;
    }

    public static ApiResult TokenError(String msg) {
        ApiResult result = new ApiResult();
        result.setCode(-1);
        result.setMsg(msg);
        return result;
    }
    public static ApiResult LimitError(String msg) {
        ApiResult result = new ApiResult();
        result.setCode(-2);
        result.setMsg(msg);
        return result;
    }
}
